<template>
  <div>
    <a-form-model
      ref="ruleForm"
      :model="params"
      layout="inline"
      class="search-form"
    >
      <a-card>
        <a-form-model-item label="模版名称" prop="name">
          <a-input v-model="params.tplName" placeholder="请输入模版名称" />
        </a-form-model-item>
        <a-form-model-item label="模版编码" prop="name">
          <a-input v-model="params.tplCode" placeholder="请输入模版编码" />
        </a-form-model-item>
        <a-form-model-item label="状态" prop="status">
          <a-select v-model="params.status" placeholder="请选择" style="width: 120px">
            <a-select-option  key="">
              全部
            </a-select-option>
            <a-select-option v-for="(value,key) in statusMap" :key="key">
              {{ value }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" @click="handleSearch">
            搜索
          </a-button>
          <a-button :style="{ marginLeft: '8px' }" @click="handleReset">
            重置
          </a-button>
        </a-form-model-item>
      </a-card>
    </a-form-model>
    <a-card>
      <div slot="extra">
        <a-button type="primary" @click="() => onEdit()">
          新建
        </a-button>
      </div>
      <a-table
         bordered
        :columns="columns"
        :row-key="record => record.id"
        :data-source="data"
        :pagination="pagination"
        :loading="loading"
        size="middle"
        @change="handleTableChange"
      >
        <template slot="ssoEnabled" slot-scope="text , record">
          <a-switch disabled size="small" :checked="record.ssoEnabled" />
        </template>
        <template slot="status" slot-scope="text , record">
          <a-badge v-if="record.status === 1" status="success" :text="statusMap[record.status]" />
          <a-badge v-else status="error" :text="statusMap[record.status]" />
        </template>
        <template slot="operation" slot-scope="text , record">
          <a @click="() => onEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="() => onDesigner(record)">设计表单</a>
          <a-divider type="vertical" />
          <a-popconfirm
            title="确定删除吗?"
            @confirm="() => onDelete(record)"
          >
            <a href="javascript:;">删除</a>
          </a-popconfirm>
        </template>
      </a-table>
    </a-card>

    <a-modal
      :title="modalTitle"
      :visible="visible"
      :confirm-loading="confirmLoading"
      destroy-on-close
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <template-form ref="form" v-model="record" @done="onSubmitDone" />
    </a-modal>
  </div>
</template>
<script>
import { statusMap, page, remove } from '@/api/formTemplate'
import TemplateForm from './components/TemplateForm'

export default {
  name: 'ProcessFormTemplate',
  components: {
    TemplateForm
  },
  data() {
    return {
      columns: [
        {
          title: '模版名称',
          dataIndex: 'tplName',
          ellipsis: true
        },
        {
          title: '模版编码',
          dataIndex: 'tplKey',
          ellipsis: true
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          ellipsis: true
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime',
          ellipsis: true,
          customRender: (text, record, index) => { return this.$utils.toDateDiffText(record.updateTime) }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      loading: false,
      confirmLoading: false,
      modalTitle: '',
      visible: false,
      pagination: { showSizeChanger: true, showTotal: total => `共 ${total} 条数据` },
      params: {
        page: 1,
        limit: 10,
        tplName: '',
        tplCode: '',
        status: ''
      },
      statusMap: statusMap,
      data: [],
      record: {}
    }
  },
  created() {
    this.handleLoad()
  },
  methods: {
    handleSearch() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.params.page = 1
          this.handleLoad()
        }
        return false
      })
    },
    handleReset() {
      this.$refs.ruleForm.resetFields()
    },
    handleTableChange(pagination, filters, sorter) {
      this.params = {
        ...this.params,
        limit: pagination.pageSize,
        page: pagination.current
      }
      this.handleLoad()
    },
    handleLoad(params) {
      this.loading = true
      if (params) {
        this.params = { ...this.params, ...params }
      }
      this.pagination.current = this.params.page
      page(this.params).then(({ data }) => {
        const pagination = { ...this.pagination }
        this.data = data.records
        pagination.total = data.total
        this.pagination = pagination
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleOk(e) {
      this.confirmLoading = true
      this.$refs.form.onSubmit()
    },
    handleCancel(e) {
      this.visible = false
    },
    onEdit(row) {
      this.modalTitle = row ? '编辑' : '新增'
      this.visible = true
      this.record = row
    },
    onSubmitDone(success) {
      this.confirmLoading = false
      if (success) {
        this.handleCancel()
        this.handleLoad()
      }
    },
    onDesigner(row) {
      const r = this.$router.resolve({ path: '/form-template/designer', query: { id: row.id }})
      window.open(r.href, '_blank')
    },
    onDelete(row) {
      remove({ id: row.id }).then(res => {
        res.success ? this.$message.success('删除成功') : this.$message.error('删除失败')
        this.handleLoad({ page: 1 })
      })
    }
  }
}
</script>
